package com.ruoyi.cmdb.controller;

import com.jcraft.jsch.JSchException;
import com.ruoyi.cmdb.util.DeleteLocalFile;
import com.ruoyi.cmdb.util.SavePrivateKeyToFile;
import com.ruoyi.cmdb.util.SshByPasswordExecution;
import com.ruoyi.cmdb.util.SshByPrivateKeyExecution;
import com.ruoyi.cmdb.vo.SshExecRes;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Path;


/**
 * 测试ssh
 *
 */
@RestController
@RequestMapping("/cmdb/testSsh")
@Api(tags = "4.testSsh")
public class TestSshController {
    @Value("${ssh.privateKey}") //私钥字符串
    private String privateKeyString;

    @Value("${randomLength}") //随机字符串长度
    private Integer randomLength;

    /**
     * testSshByPrivateKey
     */
    @GetMapping("/testSshByPrivateKey")
    @ApiOperation("testSshByPrivateKey")
    public AjaxResult testSshByPrivateKey()  throws JSchException, InterruptedException  {
        String host = "192.168.32.144";
        String user = "root";
        int port = 22; // 默认SSH端口是22
        String command = "chmod 777 /tmp/file.txt;ls -l /tmp/file.txt"; // 设置要执行的命令

        Path privateKeyPath=SavePrivateKeyToFile.save(privateKeyString,randomLength);
        String privateKeyAbsolutePath=String.valueOf(privateKeyPath.toAbsolutePath());
        SshByPrivateKeyExecution sshByPrivateKeyExecution =new SshByPrivateKeyExecution();
        SshExecRes sshExecRes= sshByPrivateKeyExecution.executeCommandWithPrivateKey(host, port, user, privateKeyAbsolutePath, command);

        DeleteLocalFile.delete(privateKeyPath);
        String rps="ssh testSsh success sout:"+sshExecRes.getOutPut()+",exitCode:"+sshExecRes.getExitCode();
        //String rps="ssh testSsh success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }
    /**
     * testSshByPassword
     */
    @GetMapping("/testSshByPassword")
    @ApiOperation("testSshByPassword")
    public AjaxResult testSshByPassword()  throws JSchException, InterruptedException  {
        String host = "192.168.32.144";
        String user = "root";
        String password = "123`abcA";
        int port = 22; // 默认SSH端口是22
        String command = "chmod 777 /tmp/file.txt;ls -l /tmp/file.txt"; // 设置要执行的命令

        SshByPasswordExecution sshByPasswordExecution =new SshByPasswordExecution();
        SshExecRes sshExecRes= null;
        try {
            sshExecRes = sshByPasswordExecution.executeCommandWithPassword(host, port, user, password, command);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String rps="ssh testSsh success sout:"+sshExecRes.getOutPut()+",exitCode:"+sshExecRes.getExitCode();
        //String rps="ssh testSsh success sout";

        System.out.println(rps);
        return AjaxResult.success(rps);
    }
}
